package com.example.mysqltry.service.impl;

import com.example.mysqltry.api.param.MallUserUpdateParam;
import com.example.mysqltry.commom.ServiceResultEnum;
import com.example.mysqltry.dao.MallUserMapper;
import com.example.mysqltry.dao.MallUserTokenMapper;
import com.example.mysqltry.entity.MallUser;
import com.example.mysqltry.entity.MallUserToken;
import com.example.mysqltry.service.BeeMallUserService;
import com.example.mysqltry.util.NumberUtil;
import com.example.mysqltry.util.SystemUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
@Service
public class BeeMallUserServiceImpl implements BeeMallUserService {

    @Autowired
    private MallUserMapper mallUserMapper;

    @Autowired
    private MallUserTokenMapper mallUserTokenMapper;
    private String getNewToken(String timeStr, Long userId) {
        String src = timeStr + userId + NumberUtil.genRandomNum(4);
        return SystemUtil.genToken(src);
    }
    @Override
    public  String login(String loginName,String passwordMD5){
         MallUser mallUser=mallUserMapper.selectByLoginnameAndPasswd(loginName,passwordMD5);
         if(mallUser!=null) {
             if (mallUser.getLockedFlag() == 0) {
                 ServiceResultEnum.LOGIN_USER_LOCKED_ERROR.getResult();
             }
             String token=getNewToken(System.currentTimeMillis()+"",mallUser.getUserId());
             MallUserToken mallUserToken=mallUserTokenMapper.selectByPrimaryKey(mallUser.getUserId());
             Date now=new Date();
             Date expireTime=new Date(now.getTime()+2*24*3600*1000);
             if(mallUserToken==null)
             {
                 mallUserToken = new MallUserToken();
                 mallUserToken.setUserId(mallUser.getUserId());
                 mallUserToken.setToken(token);
                 mallUserToken.setUpdateTime(now);
                 mallUserToken.setExpireTime(expireTime);
                 if(mallUserTokenMapper.insertSelective(mallUserToken)>0)
                 {return  token;}

             }
             else {
                 mallUserToken.setToken(token);
                 mallUserToken.setUpdateTime(now);
                 mallUserToken.setExpireTime(expireTime);
                 //更新
                 if (mallUserTokenMapper.updateByPrimaryKeySelective(mallUserToken)>0) {
                     //修改成功后返回
                     return token;
                 }
             }
         }
         return ServiceResultEnum.LOGIN_ERROR.getResult();



    }

    @Override
    public Boolean updateUser(MallUserUpdateParam mallUserUpdateParam, Long userId)
    {
        MallUser mallUser=mallUserMapper.selectByPrimaryKey(userId);
        if(mallUser==null)
        {
            ServiceResultEnum.LOGIN_NAME_NULL.getResult();
        }
        mallUser.setNickName(mallUserUpdateParam.getNickName());
        mallUser.setIntroduceSign(mallUserUpdateParam.getIntroduceSign());
        mallUser.setPasswordMd5(mallUserUpdateParam.getPasswordMd5());
        if(mallUserMapper.updateByPrimaryKeySelective(mallUser)>0)
        {
            return  true;
        }
        return  false;
    }

    public Boolean logoutUser(MallUser mallUser)
    {
        return mallUserTokenMapper.deleteByPrimaryKey(mallUser.getUserId())>0;
    }

}
